package com.xunji.standard.common;


/**
 * 公共返回类
 */
public class R<T> {

    private Integer code;

    private String msg;

    private T data;

    private R() {
    }

    ;

    public R(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static R success() {
        R result = new R();
        result.setResultCode(ResultCodeEnum.SUCCESS);
        return result;
    }

    @SuppressWarnings("unchecked")
    public static <T> R success(T data) {
        R result = new R();
        result.setResultCode(ResultCodeEnum.SUCCESS);
        result.setData(data);
        return result;
    }

    @SuppressWarnings("unchecked")
    public static R success(Object msg, Object data) {
        R result = new R();
        result.setResultCode(ResultCodeEnum.SUCCESS);
        result.setMsg((String) msg);
        result.setData(data);
        return result;
    }

    public static R failure(ResultCodeEnum resultCode) {
        R result = new R();
        result.setResultCode(resultCode);
        return result;
    }

    public static R failure(String msg) {
        R result = new R();
        result.code = ResultCodeEnum.FAIL.getCode();
        result.msg = msg;
        return result;
    }

    public static R failure(int code, String msg) {
        R result = new R();
        result.code = code;
        result.msg = msg;
        return result;
    }

    @SuppressWarnings("unchecked")
    public static R failure(ResultCodeEnum resultCode, Object data) {
        R result = new R();
        result.setResultCode(resultCode);
        result.setData(data);
        return result;
    }

    @SuppressWarnings("unchecked")
    public static R failure(Object code, Object data) {
        R result = new R();
        result.setCode((Integer) code);
        result.setData(data);
        return result;
    }

    public static R defaultFailure() {
        R result = new R();
        result.setResultCode(ResultCodeEnum.FAIL);
        return result;
    }


    public void setResultCode(ResultCodeEnum codeEnum) {
        this.code = codeEnum.getCode();
        this.msg = codeEnum.getMessage();
    }

    ;

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

}
